<?php
/**
 * 页面控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace web;

class Controller_Article extends Controller_BaseController {

    public $template = "template";

    
    public function before(){
        parent::before();

        $seller = \Session::get('seller');
        $main_menu_id = \Input::get('m',$seller->main_menu);//主导航大类菜单ID
        $left_menu_id = \Input::get('l',$seller->article_menu);//左侧导航大类菜单ID
        $bottom_menu_id = \Input::get('b',$seller->bottom_menu);//底部导航大类菜单ID
        $current_menu_id = \Input::get('c',0);//

        $menu_root = \Model_Category::find($current_menu_id);
        $params['title'] = isset($menu_root->name) ? $menu_root->name : '';
        $params['current_menu'] = $menu_root;//面包屑   首页>parent>current

        //主菜单
        $menu_root = \Model_Category::find($main_menu_id);
        $menu = $menu_root->children()->get();
        $items = array();
        if($menu){
            $items = \utils\CategoryHandler::get_children($menu);
        }

        $params['nav_menu'] = $items;
        unset($menu_root);
        unset($menu);

        //左侧菜单
        $menu_root = \Model_Category::find($left_menu_id);
        $params['left_menu'] = $menu_root;//左侧菜单
        $menu = $menu_root->children()->get();
        $items = array();
        if($menu){
            $items = \utils\CategoryHandler::get_children($menu);
        }
        $params['left_menus'] = $items;
        unset($menu_root);
        unset($menu);

        //底部菜单
        $menu_root = \Model_Category::find($bottom_menu_id);
        $left_menu = $menu_root->children()->get();

        //组合2-3级菜单
        $items = array();
        if($left_menu){
            $items = \utils\CategoryHandler::get_children($left_menu);
        }
        $params['bottom_menu'] = $items;
        \View::set_global($params);
    }

    public function action_index($id=0){
        $params = array(
            'title' => '文章',
            'menu' => \Input::get('m',0),
            'l_menu' => \Input::get('l',0),
        );

        $seller = \Session::get('seller');

        $id = $id ? $id: \Input::get('nid',0);
        if(!$id){

            die('系统繁忙！');
        }

        $params['item'] = \Model_Node::query()
                        ->related(array('category'))
                        ->where(array('id'=> $id, 'is_delete' => 0))
                        ->get_one();
        $params['title'] = isset($params['item']->category->name) ? $params['item']->category->name : $params['title'];

        \View::set_global($params);
        $this->template->content = \View::forge("article");
    }

    public function action_list($id = 0){

        $params = array(
            'menu' => 'INDEX',
        );
        
        $items = \Model_Node::query()
                        ->related(array('category'))
                        ->where(array('category_id'=> $id, 'is_delete' => 0))
                        ->order_by(array('stick' => 'desc', 'id'=>'desc'));
        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/web/article/list/".$id,
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 8),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'jinse'
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();

        \View::set_global($params);
        $this->template->content = \View::forge("article/list");
    }

     public function action_detail($id = 0){

        $params = array(
            'title' => '文章详情',
            'menu' => 'INDEX',
        );

        $id = $id ? $id: \Input::get('nid',0);

        $params['item'] = \Model_Node::query()
                        ->related(array('category'))
                        ->where(array('id'=> $id, 'is_delete' => 0))
                        ->get_one();
        $params['title'] = isset($params['item']->category->name) ? $params['item']->category->name : $params['title'];
        
        \View::set_global($params);
        $this->template->content = \View::forge("article/detail");
    }

}